package com.yunxi.poc.starbucks.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunxi.poc.starbucks.common.BaseController;
import com.yunxi.poc.starbucks.common.BizConstant;
import com.yunxi.poc.starbucks.entity.Tb1TransactionSale;
import com.yunxi.poc.starbucks.service.ITb1TransactionSaleService;
import com.yunxi.poc.starbucks.vo.BuyVO;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author shiyuan.xlc
 * @since 2021-08-09
 */
@RestController
@RequestMapping("/starbucks/transaction_sale")
public class Tb1TransactionSaleController extends BaseController {

    @Resource
    ITb1TransactionSaleService tb1TransactionSaleService;

    @ResponseBody
    @RequestMapping(value = {""}, method = RequestMethod.POST)
    public ResponseEntity list(@RequestBody BuyVO buyerInfo, Page pageInfo) {
        QueryWrapper<Tb1TransactionSale> queryWrapperTrans = new QueryWrapper<>();
        queryWrapperTrans.select("sale_item_quantity", "sale_datetime");
        queryWrapperTrans.in("global_store_number", buyerInfo.getStoreIds());
        queryWrapperTrans.ge("sale_datetime", buyerInfo.getStart());
        queryWrapperTrans.le("sale_datetime", buyerInfo.getEnd());
        queryWrapperTrans.last("AND sale_item_code "
                + "in ( SELECT distinct sale_item_code FROM saleitem_with_reportcategory "
                + "where report_category in ("
                + BizConstant.REPORT_CATEGORY.stream().map(e -> "'" + e + "'").collect(Collectors.joining(","))
                + ") )");
        Page<Tb1TransactionSale> tb1TransactionSales = this.tb1TransactionSaleService.page(pageInfo, queryWrapperTrans);
        return ResponseEntity.status(HttpStatus.OK).body(tb1TransactionSales);
    }

}
